package com.astrob.model;

import com.astrob.naviframe.Start;

import java.util.ArrayList;

public class HistoryFileHandle {
	private static String HISTORYPATH = Start.RUNDIR + "/historydest.dat";
	private static HistoryFileHandle instance = new HistoryFileHandle();
	private ArrayList<HistoryData> mListHistory = new ArrayList<HistoryData>();

	public static HistoryFileHandle getInstance() {
		return instance;
	}

	public ArrayList<HistoryData> getListHistory() {
		return mListHistory;
	}

	public HistoryFileHandle() {
		load();
	}

	public void load() {
		mListHistory.clear();
		ReadObjectsFromFile<HistoryData> readFile = new ReadObjectsFromFile<HistoryData>();
		readFile.onReadObjectsList(HISTORYPATH, mListHistory);
		reFilterNative();
	}

	@SuppressWarnings("unchecked")
	public void save() {
		@SuppressWarnings("rawtypes")
		WriteObjectsToFile writeFile = new WriteObjectsToFile();
		writeFile.onWriteObjects(HISTORYPATH, mListHistory);
	}

	public boolean add(HistoryData data) {
		if (data == null)
			return false;

//		data.countryID = "CN";
		int index = mListHistory.indexOf(data);
		if (index >= 0) {
			delete(index);
		}
		
		if (data.name.length() < 1 || data.name.compareTo("当前位置") == 0) {
			data.name = Start.getInstance().MapKit().getposInfo(new LonLat(data.lon, data.lat));
			String name = data.name.replace("*", " ");
			name = name.trim();
			data.name = name;
		}
		
		data.setCurrentTimeMillis(System.currentTimeMillis());
		mListHistory.add(0, data);
		// 历史目的地不超过50个
		if (mListHistory.size() > 500) {
			delete(mListHistory.size() - 1);
		}
		save();
		reFilterNative();
		return true;
	}

	public void delete(int index) {
		if (index >= 0 && index < mListHistory.size()) {
			mListHistory.remove(index);
		}
		reFilterNative();
	}

	public boolean delete(HistoryData data) {
		if (data == null || mListHistory.size() < 1) {
			return true;
		}
		boolean ret = mListHistory.remove(data);
		reFilterNative();
		return ret;
	}

	public boolean haveSameFav(HistoryData data) {
		if (data == null || mListHistory.size() < 1) {
			return false;
		}
		return mListHistory.contains(data);
	}

	public void removeAll() {
		// mListHistory.clear();
		ArrayList<HistoryData> listHistory = new ArrayList<HistoryData>();
		for (HistoryData data : mListHistory) {
			listHistory.add(data);
		}
		
		for (int i = 0; i < listHistory.size(); i++) {
			HistoryData data = listHistory.get(i);
//			if (data.countryID.compareToIgnoreCase("CN") == 0) {
				mListHistory.remove(data);
//			}
		}
		reFilterNative();
		save();
	}
	
	private String curCountry = null;
	ArrayList<HistoryData> mNativeHistory = new ArrayList<HistoryData>();
	//当前国家ID
	public ArrayList<HistoryData> getNativeHistory(){
		if(curCountry == null){
			curCountry = "CN";
			reFilterNative();
		}
		if(curCountry.compareToIgnoreCase("CN") == 0){
			//do no thing
		}
		else{
			curCountry = "CN";
			reFilterNative();
		}
		return mNativeHistory;
	}
	public void clearNativeHistory(){
		mNativeHistory.clear();
		int size = mListHistory.size();
		for(int i = size - 1; i >= 0; i--){
			HistoryData data = mListHistory.get(i);
			if(data.countryID.compareToIgnoreCase(curCountry) == 0){
				mListHistory.remove(i);
			}
		}
	}
	private void reFilterNative() {
		mNativeHistory.clear();
		if(curCountry == null){
			return;
		}
		for(HistoryData data : mListHistory){
			if(data.countryID.compareToIgnoreCase(curCountry) == 0){
				mNativeHistory.add(data);
			}
		}		
	}
}
